<template>
  <div class="com-container" @dblclick="revertMap">
    <div class="com-chart" ref="map_ref"></div>
  </div>
</template>

<script>
import axios from 'axios'
import { getProvinceMapInfo } from '@/utils/map_utils'
import { mapState } from 'vuex'

export default {
  data() {
    return {
      chartInstance: null,
      allData: [],
    }
  },
  computed: {
    ...mapState(['theme']),
  },
  watch: {
    theme() {
      this.chartInstance.dispose()
      this.initChart()
      this.screenAdapter()
      this.updateChart()
    },
  },
  created() {
    this.$socket.registerCallBack('mapData', this.getData)
  },
  mounted() {
    this.initChart()
    // this.getData()
    this.$socket.send({
      action: 'getData',
      socketType: 'mapData',
      chartName: 'map',
    })
    window.addEventListener('resize', this.screenAdapter)
    this.screenAdapter()
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.screenAdapter)
    this.$socket.unRegisterCallBack('mapData')
  },
  methods: {
    async initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.map_ref, this.theme)

      const { data: mapData } = await axios.get('http://localhost:8999/static/map/china.json')
      this.$echarts.registerMap('china', mapData)

      const initOption = {
        title: { text: '▎ 商家分布', left: 20, top: 20 },
        legend: {
          left: '5%',
          bottom: '5%',
          orient: 'vertical',
        },
        geo: [
          {
            type: 'map',
            map: 'china',
            top: '5%',
            bottom: '5%',
            itemStyle: {
              areaColor: '#2E72BF',
              borderColor: '#333',
            },
          },
        ],
      }
      this.chartInstance.setOption(initOption)
      this.chartInstance.on('click', async (args) => {
        const provinceInfo = getProvinceMapInfo(args.name)
        const { data } = await axios.get('http://localhost:8999' + provinceInfo.path)
        this.$echarts.registerMap(provinceInfo.key, data)
        this.chartInstance.setOption({
          geo: {
            map: provinceInfo.key,
          },
        })
      })
      // this.getData()
    },
    async getData() {
      // 把返回的对象中的data对应的值赋值给res这个变量
      const { data: res } = await this.$http.get('map')
      // 这里只做接收数据并存好的操作，进一步的处理全都放到updateChart里去做
      this.allData = res
      this.updateChart()
    },
    updateChart() {
      const legend = this.allData.map((item) => {
        return item.name
      })
      const series = this.allData.map((item) => {
        return {
          type: 'effectScatter',
          coordinateSystem: 'geo',
          name: item.name,
          data: item.children,
          rippleEffect: {
            scale: 5,
            brushType: 'stroke',
          },
        }
      })
      const updateOption = {
        legend: {
          data: legend,
        },
        series: series,
      }
      this.chartInstance.setOption(updateOption)
    },
    screenAdapter() {
      const sizeValue = (this.$refs.map_ref.offsetWidth / 100) * 3.6
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: sizeValue,
          },
        },
        legend: {
          itemWidth: sizeValue / 2,
          itemHeight: sizeValue / 2,
          itemGap: sizeValue / 2,
          textStyle: {
            fontSize: sizeValue / 2,
          },
        },
      }
      this.chartInstance.setOption(adapterOption)
      this.chartInstance.resize()
    },
    revertMap() {
      this.chartInstance.setOption({
        geo: {
          map: 'china',
        },
      })
    },
  },
}
</script>

<style></style>
